<?php
class tplAgenda
{
	public static function dates($attr)
	{
		global $core, $_ctx;

		//$res ='<?php	
		$format = "";
		
		if (!empty($attr["format"])) {
			$format = addslashes($attr["format"]);
		}
		
		$iso8601 = !empty($attr["iso8601"]);
		$rfc822 = !empty($attr["rfc822"]);
		
		$f = $core->tpl->getFilters($attr);
		
		$dt_deb = strtotime($_ctx->posts->date_debut);
		$dt_fin = strtotime($_ctx->posts->date_fin);
		if ($rfc822) {
			$dt_deb = dt::rfc822($dt_deb, $_ctx->posts->post_tz);
			$dt_fin = dt::rfc822($dt_fin, $_ctx->posts->post_tz);
		} elseif ($iso8601) {
			$dt_deb = dt::iso8601($dt_deb, $_ctx->posts->post_tz);
			$dt_fin = dt::iso8601($dt_fin, $_ctx->posts->post_tz);

		} else {
			if ($format) {
				$dt_deb = dt::str($format,$dt_deb);
				$dt_fin = dt::str($format,$dt_fin);
			} else {
				$dt_deb = dt::str($core->blog->settings->date_format,$dt_deb);
				$dt_fin = dt::str($core->blog->settings->date_format,$dt_fin);
			}
		}
		$res = __("From")." ".$dt_deb." ".__("to")." ".$dt_fin; 
		return $res;
	}
	
	public static function AgendaTitre($attr){
		global $core, $_ctx;
		if (preg_match('`.*Agenda/(.*)`', $_SERVER['REQUEST_URI'], $matchs))
		{	
			$dat = '';
			if (substr($matchs[1], -1) == '/'){
				$dat = substr($matchs[1], 0, -1);
			}else{
				$dat = $matchs[1];
			}
			
			$days = explode('/', $dat);
			switch(count($days))
			{
				case 3:
					$ret = __('of day');
					break;
				case 2:
					$ret = __('of month');
					break;
				case 1:
					$ret = __('of year');
					break;
				case 0:
					$ret = __('all');
					break;
			}
		/*	$timestamp = mktime(0, 0,0, $days[1], $days[2], $days[0]);
			$fin =  mktime(23, 59,59, $days[1], $days[2], $days[0]);
		*/
			return __('Events').' '.$ret.' : '.$dat;
		}else{
			$ret = 'tout';
			return __('Agenda de').' : '.$core->blog->name;
		}		
	}
	
	/**
	 * Retourne les entrées de l'agenda ...
	 *
	 * @param Array $attr les attributs
	 * @param string $content
	 * @return void
	 */
	public static function AgendaEntries($attr, $content){
		global $core;
		/*
		 * Commenté car il me manque des billes pour gérer la pagination
		$lastn = 0;
		if (isset($attr['lastn'])) {
			$lastn = abs((integer) $attr['lastn'])+0;
		}
		$p = 'if (!isset($_page_number)) { $_page_number = 1; }'."\n";
		
		if ($lastn > 0) {
			$p .= "\$params['limit'] = ".$lastn.";\n";
		} else {
			$p .= "\$params['limit'] = \$_ctx->nb_entry_per_page;\n";
		}
		if (!isset($attr['ignore_pagination']) || $attr['ignore_pagination'] == "0") {
			$p .= "\$params['limit'] = array(((\$_page_number-1)*\$params['limit']),\$params['limit']);\n";
		} else {
			$p .= "\$params['limit'] = array(0, \$params['limit']);\n";
		}
		$sortby = 'G.date_debut';
		$order = 'asc';
		if (isset($attr['sortby'])) {
			switch ($attr['sortby']) {
				case 'title': $sortby = 'post_title'; break;
				case 'selected' : $sortby = 'post_selected'; break;
				case 'author' : $sortby = 'user_id'; break;
				case 'date' : $sortby = 'post_dt'; break;
			}
		}
		if (isset($attr['order']) && preg_match('/^(desc|asc)$/i',$attr['order'])) {
			$order = $attr['order'];
		}
		$p .= "\$params['order'] = '".$sortby." ".$order."';\n";
		$p .= "\$params['post_type'] = 'agenda';\n";
		$res = "<?php\n";
		$res .= $p;
		$res .= '$_ctx->post_params = $params;'."\n";
		$res .= '$agenda = new dcAgendaPlugin($core);'."\n";
		$res .= '$_ctx->posts = $agenda->getPosts($params);unset($params);'."\n";
		$res .= "?>\n";
		*/
		$res = '<?php while ($_ctx->posts->fetch()) : ?>'.
				$content.
				
				'<?php endwhile; ?>';
		return $res;
	}
	
	/**
	 * Affiche un lien vers le jour precedent
	 * @return void
	 */
	public static function AgendaPrevious(){
		global $core, $_ctx;
		if (preg_match('`.*Agenda/(.*)`', $_SERVER['REQUEST_URI'], $matchs))
		{	
			$dat = '';
			if (substr($matchs[1], -1) == '/'){
				$dat = substr($matchs[1], 0, -1);
			}else{
				$dat = $matchs[1];
			}
			
			
		
			
			$days = explode('/', $dat);
			if (($days[0] % 4) == 0){
				$nbrjour = array(0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	 		}else{
	 			$nbrjour = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
			}
			
			switch(count($days))
			{
				case 3:
					//$ret = 'jour';
					if ($days[2] == 1){
						// Je vais au moi precedent, 
						if ($days[1] == 1)
						{
							$jour =31;
							$mois = 12;
							$annee = $days[0]-1;
						}else{
							
							$mois = $days[1]-1;
							$jour = $nbrjour[$mois];
							$annee = $days[0];
						}
					}else{
						$jour = $days[2]-1;
						$mois = $days[1];
						$annee = $days[0];
					}
					$ret = '<a href="'.widgetsAgenda::constructUrl($jour, $mois, $annee).'">';
					$ret .= date($GLOBALS['agenda_format_date_jour_suivant'], mktime(0,0,0, $mois, $jour, $annee ) );
					$ret .= '</a>';
					break;
				case 2:
					if ($days[1] == 1)
					{
						$mois = 12;
						$annee = $days[0]-1;
					}else{
						$mois = $days[1]-1;
						$annee = $days[0];
					}
					
					$ret = '<a href="'.widgetsAgenda::constructUrl(0, $mois, $annee).'">';
					$ret .= date($GLOBALS['agenda_format_date_mois_suivant'], mktime(0,0,0, $mois,1, $annee ) );
					$ret .= '</a>';
					break;
				case 1:
					$annee = $days[0]-1;
					$ret = '<a href="'.widgetsAgenda::constructUrl(0, 0, $annee).'">';
					$ret .= date($GLOBALS['agenda_format_date_annee_suivant'], mktime(0,0,0, $mois,1, $annee ) );
					$ret .= '</a>';
					break;
				case 0:
					$ret = 'precedent pas normal a coder';
					break;
			}
			return $ret;
		}		
	}
	
	/**
	 * Affiche un lien vers le jour suivant
	 * @return void
	 */
	public static function AgendaNext(){
		global $core, $_ctx;
		if (preg_match('`.*Agenda/(.*)`', $_SERVER['REQUEST_URI'], $matchs))
		{	
			$dat = '';
			if (substr($matchs[1], -1) == '/'){
				$dat = substr($matchs[1], 0, -1);
			}else{
				$dat = $matchs[1];
			}
			
			$days = explode('/', $dat);
			if (($days[0] % 4) == 0){
				$nbrjour = array(0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	 		}else{
	 			$nbrjour = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
			}
			
			switch(count($days))
			{
				case 3:
					//$ret = 'jour';
					if ($nbrjour[$days[1]] == $days[2])
					{
						// mois suivant
						if ($days[1] == 12){
							// Annee suivante
							$jour = 1;
							$mois = 1;
							$annee = $days[0]+1;
						}else{
							$jour = 1;
							$mois = $days[1]+1;
							$annee = $days[0];
						}
					}else{
						// Mois en cours
						$jour = $days[2]+1;
						$mois = $days[1];
						$annee = $days[0];
					}
					
					
					$ret = '<a href="'.widgetsAgenda::constructUrl($jour, $mois, $annee).'">';
					$ret .= date($GLOBALS['agenda_format_date_jour_suivant'], mktime(0,0,0, $mois, $jour, $annee ) );
					$ret .= '</a>';
					break;
				case 2:
					if ($days[1] == 12)
					{
						$mois = 1;
						$annee = $days[0]+1;
					}else{
						$mois = $days[1]+1;
						$annee = $days[0];
					}
					
					$ret = '<a href="'.widgetsAgenda::constructUrl(0, $mois, $annee).'">';
					$ret .= date($GLOBALS['agenda_format_date_mois_suivant'], mktime(0,0,0, 1, 1, $annee ) );
					$ret .= '</a>';
					break;
				case 1:
					$annee = $days[0]+1;
					$ret = '<a href="'.widgetsAgenda::constructUrl(0, 0, $annee).'">';
					$ret .= date($GLOBALS['agenda_format_date_annee_suivant'], mktime(0,0,0, 1,1, $annee ) );
					$ret .= '</a>';
					break;
				case 0:
					$ret = 'Suivant pas normal a coder';
					break;
			}
			return $ret;
		}		
		
	}
	
	/**
	 * Retourne le lien vers tous les evenements enregistrés
	 *
	 * @return string
	 */
	public static function getLinkToAgenda(){
		global $core;
		return $core->blog->url.'Agenda';  
	}
}
